package cn.xd.core.response;

import cn.hutool.core.collection.CollUtil;
import cn.xd.common.exception.BizException;
import cn.xd.core.exception.DBException;

import java.util.Collection;
import java.util.function.Consumer;

/**
 * @author : mirzaLin
 * @data : 2023/11/19
 * @description : DB多条 数据结果
 **/
public class CollectResult<T> {
    private final Collection<T> res;

    private CollectResult(Collection<T> res) {
        this.res = res;
    }

    public static <T> CollectResult<T> of(Collection<T> ab) {
        return new CollectResult<>(ab);
    }

    public Collection<T> get() {
        return res;
    }


    public Collection<T> isEmptyThrow(RuntimeException exception) throws RuntimeException {
        if (CollUtil.isEmpty(res)) {
            throw exception;
        }
        return res;
    }

    public Collection<T> isEmptyThrow(String msg) throws BizException {
        return isEmptyThrow(new BizException(msg));
    }

    public Collection<T> isEmptyThrow() {
        return isEmptyThrow(new DBException("db异常"));
    }

    public Collection<T> isEmpty(Function consumer) {
        if (CollUtil.isEmpty(res)) {
            consumer.execute();
        }
        return res;
    }

    public Collection<T> isNotEmptyThrow(RuntimeException exception) throws RuntimeException {
        if (CollUtil.isNotEmpty(res)) {
            throw exception;
        }
        return res;
    }

    public Collection<T> isNotEmptyThrow(String msg) throws BizException {
        return isNotEmptyThrow(new BizException(msg));
    }

    public Collection<T> isNotEmpty(Consumer<Collection<T>> consumer) {
        if (CollUtil.isNotEmpty(res)) {
            consumer.accept(res);
        }
        return res;
    }
}
